<template>
	<div style="width: 600px;text-align: center;margin: 0 auto;">
		<h1>正在编辑【{{linkForm.name}}】TDengie连接信息</h1>
		<el-form :model="linkForm" :ref="linkForm" :rules="linkFormRules" label-width="100px">
			<el-form-item label="名称" prop="name">
				<el-input v-model="linkForm.name"></el-input>
			</el-form-item>
			<el-form-item label="主机地址" prop="host">
				<el-input v-model="linkForm.host"></el-input>
			</el-form-item>
			<el-form-item label="端口" prop="port">
				<el-input v-model="linkForm.port"></el-input>
			</el-form-item>
			<el-form-item label="用户名" prop="user">
				<el-input v-model="linkForm.user"></el-input>
			</el-form-item>
			<el-form-item label="密码" prop="password">
				<el-input type="password" v-model="linkForm.password"></el-input>
			</el-form-item>
		</el-form>
		<div slot="footer" class="dialog-footer">
			<el-button type="primary" plain style="width: 100%;" @click="confirmAddLink" size="medium">确 定</el-button>
		</div>
	</div>
</template>
<script>
	import {mapGetters} from 'vuex'
	import * as taos from '../api/taosrestful'
	import bus from '../utils/bus'
	
	export default {
		name: "taoslink",
		data() {
			return {
				linkForm: {'name':'','host':'','port':'6041','user':'','password':''},
				linkFormRules:{
					name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
					host: [{ required: true, message: '请输入主机地址', trigger: 'blur' }],
					port: [{ required: true, message: '请输入端口', trigger: 'blur' }],
					user: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
					password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
				},
			}
		},
		created() {
			// this.init()
		},
		computed: {
			...mapGetters(['curObject'])
		},
		beforeDestroy () {//页面销毁时需要解绑
		    bus.$off("changeCurObject_link");
		},
		mounted() {
			var that = this;
			bus.$on('changeCurObject_link', function(p) {
				that.init()
			})
		},
		methods: {
			init() {
				this.linkForm = this.curObject.link
			},
			confirmAddLink() {
				localStorage.clear()
				this.linkForm.id = new Date().getTime()
				this.linkForm.hasChildren = true
				this.linkForm.type = 'link'
				this.$store.dispatch("addDbLink", this.linkForm)
					.then(() => {
						this.$message({message: '修改成功',type: 'success',duration: 1000})
					})
					.catch((e) => {
						console.error(e)
					})
			}
		}
	}
</script>
